package org.irri.smta.report.result;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class SmtaReportResultBuilder {

	public ObjectFactory smtaReportResultFactory = new ObjectFactory();

	public SmtaReportResult createSmtaReportResultShell() {
		SmtaReportResult smtaReportResult = smtaReportResultFactory.createSmtaReportResult();
		return smtaReportResult;
	}
	
	public String createXml(SmtaReportResult smtaReportResult, String encoding) throws Exception {
		XStream xstream = new XStream();
		xstream.alias("smta", SmtaReportResult.class);
		StringBuffer xmlbuf = new StringBuffer();
		xmlbuf.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
		xmlbuf.append(System.getProperty("line.separator"));
		xmlbuf.append(xstream.toXML(smtaReportResult));
		String xml = xmlbuf.toString();
		byte[] utf8Bytes = xml.getBytes(encoding);
		String utf8Xml = new String(utf8Bytes, encoding);
		return utf8Xml;
	}
	
	public SmtaReportResult unmarshalXml(String xml) {
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("smta", SmtaReportResult.class);
		xstream.alias("error", SmtaReportResult.Error.class);
		SmtaReportResult smtaReportResult = (SmtaReportResult) xstream.fromXML(xml);
		return smtaReportResult;
	}

}
